<?php
/**
 * 获取用户是否已经是第一次看引导动画了
 */
require_once dirname(__DIR__)."/login_filter.php";
//require_once dirname(dirname(__DIR__))."/common.php";
use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\cls\tom\Tools;
use app\cls\sdk\FoundaoVideoSDK;
use app\cls\tom\FFmpeg;
use app\cls\tom\Config;
use app\cls\upload\SingleUpload;
use app\model\Video_templet;
use app\model\UserVideo;

//实例化数据库
$db = SysResource::getDB(false);
$Video_templet = new Video_templet($db);
//用户uuid
$uuid=$Auth['uuid'];
//先去查询视频模板
$result=$Video_templet->get_video_templet();
if(empty($result)){
    Response::output(-1,"系统出了点小问题，难过");
}
//
$FoundaoVideoSDK=new FoundaoVideoSDK();
//设置视频参数
$option1=[];
$option2=[];
$option3=[];

$len=count($result);
if($len==1){
    $arr1=$result[0];
    $videoUrl=$result[0]['video_url'];
    $video_arrs['img_url']=$video_url;
    $times=$result[0]['video_time'];
    $video_uuid=$result[0]['video_uuid'];
    $video_desc=$result[0]['video_title'];
    $gunalian_img_url=$result[0]['video_guanlian_pic'];
    $daping_img_url=$result[0]['video_daping_pic'];
    $share_img_url=$result[0]['video_share_pic'];
    $fenlei=$result[0]['fenlei'];
    $huati=$result[0]['huati'];
}else{
    //随机取一个整数
    $arrs=range(0,$len-1);
    $num=array_rand($arrs);
    $arr1=$result[$num];
    //
    $videoUrl=$arr1['video_url'];
    $video_arrs['img_url']=$video_url;
    $times=$arr1['video_time'];
    $video_uuid=$arr1['video_uuid'];
    $video_desc=$arr1['video_title'];
    $gunalian_img_url=$arr1['video_guanlian_pic'];
    $daping_img_url=$arr1['video_daping_pic'];
    $share_img_url=$arr1['video_share_pic'];
    $fenlei=$arr1['fenlei'];
    $huati=$arr1['huati'];
}
//截取封面
$ffmpeg = new FFmpeg(false);
$img_path=upload_dir("image",'jpg');
$re=$ffmpeg->getCoverFromVideo1($videoUrl,$img_path['savepath']);
//添加数据到数据库
$data['material_id']="超级测测测";
$data['video_uuid']=$video_uuid;
$data['uuid']=$Auth['uuid'];
$data['nick_name']=$Auth['nick_name'];
$data['nick_pic']=$Auth['nick_pic'];
if(!empty($video_desc)){
    $data['video_desc']=htmlspecialchars($video_desc);
}
$data['join_type']=$fenlei;
$data['join_type_sub']=$huati;
$data['video_url']=$videoUrl;
$data['type']=2;
$data['is_dub']=1;
$data['is_job_status']=1;
$data['del1']=1;
$data['examine']=1;
$data['mix_status']=1;
$data['pic']=$gunalian_img_url;
$data['share_pic']=$share_img_url;
$data['hengping_pic']=$daping_img_url;
$data['first_pic3']=$img_path['savehttp'];

$data['add_time']=date("Y-m-d H:i:s",time());
$db = SysResource::getDB(true);
$UserVideo=new UserVideo($db);
$res=$UserVideo->add_material($data);
if($res){
    $arr1['job_id']=$res;
    Response::output(0,"添加成功2",$arr1,$_SERVER['SERVER_ADDR']);
}else{
    Response::output(-1,"添加失败");
}



//获取上传目录
function upload_dir($type="tmp",$suffix="jpg"){
    //解决跨域问题
    $allUploadConfig = Config::load("cos_upload");

    $uploadConfig = $allUploadConfig[$type];

    //创建上传目录，配置上传
    $date1=date("Y",time());
    $date2=date("m",time());
    $date3=date("d",time());
    $dir="/".$date1."/".$date2."/".$date3;
    $uploadpath1=$uploadConfig['savepath'];
    $uploadpath2=$uploadConfig['savehttp'];
    $key1=rand(1000,10000);
    $key2=rand(10000,20000);
    $key3=rand();
    $file_name=$key1.$key2.$key3;
    //这是服务器上的上传地址
    $uploadpath['savepath']=$uploadpath1.$dir."/".$file_name.".{$suffix}";
    $uploadpath['savename']=$dir."/".$file_name.".{$suffix}";
    $uploadpath['movename']=$uploadpath1.$dir;
    if($type=="video"){
        mkdir($uploadpath1.$dir."/".$file_name,0777,true);
    }
    //这是网络访问地址
    $uploadpath['savehttp']=$uploadpath2.$dir."/".$file_name.".{$suffix}";
    return $uploadpath;
}
